package huahua.pm25.service.Impl;

import huahua.pm25.mapper.Pm25Mapper;
import huahua.pm25.pojo.Pm25;
import huahua.pm25.service.Pm25Service;
import huahua.pm25.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @author: zhanghuadi
 * @date: 2018/4/27 0027
 * @description: pm25服务接口实现类
 */
@Service
public class Pm25ServiceImpl implements Pm25Service {

    @Autowired
    private Pm25Mapper pm25Mapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void addValue(Integer value) {
        Date date = DateUtil.getDate();
        pm25Mapper.insert(new Pm25(date, value));
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Pm25> selectValue(Date startTime, Date endTime) {

        //设置查询条件
        Example example = new Example(Pm25.class);
        Example.Criteria criteria = example.createCriteria();

        //查询指定时间段数据
        criteria.andBetween("updatetime",startTime,endTime);
        example.orderBy("updatetime").asc();

        return pm25Mapper.selectByExample(example);
    }
}
